package com.smm.web.vmc.handler;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class MyEncoder extends ProtocolEncoderAdapter {

    private final CharsetEncoder charsetEncoder;
    public MyEncoder(String _charsetName){
        this.charsetEncoder = Charset.forName(_charsetName).newEncoder();
    }

    @Override
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);
        buffer.putString(message.toString()+"\n", this.charsetEncoder);
        buffer.flip();
        out.write(buffer);
    }
}
